package com.aplink.generic.widgets;

import android.view.View;
import android.widget.Button;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.objectrule.ObjectBaseRule;
import com.aplink.generic.util.objectrule.ObjectLoadBaseRule;
import com.aplink.generic.util.objectrule.ObjectNotLoadBaseRule;

public class CustomButton extends BaseView {

    private final Button mButton;

    public CustomButton( GenericFragment dataEntryActivity,
                         ControlSpec controlSpec, View view ) {
        super( dataEntryActivity, controlSpec, view );
        this.mButton = (Button) view;
    }

    @Override
    protected void loadDataToView() {
        final ObjectBaseRule objBaseRule = getControlSpec().getObjectBaseRule();
        if ( objBaseRule != null ) {

            if ( objBaseRule instanceof ObjectLoadBaseRule ) {
                ObjectLoadBaseRule objLoadBaseRule = new ObjectLoadBaseRule();
                objLoadBaseRule = (ObjectLoadBaseRule) objBaseRule;

                final boolean isSatisfied = objLoadBaseRule
                        .isSatisfiedCondition( getPassedGenericObject(), - 1 );
                if ( isSatisfied ) {
                    this.mButton.setVisibility( View.VISIBLE );
                }
            }
        }
    }

    @Override
    protected void doIfNotHashPassedValues() {
        final ObjectBaseRule objBaseRule = getControlSpec().getObjectBaseRule();
        if ( objBaseRule != null ) {
            if ( objBaseRule instanceof ObjectNotLoadBaseRule ) {
                ObjectNotLoadBaseRule objNotLoadBaseRule = new ObjectNotLoadBaseRule();
                objNotLoadBaseRule = (ObjectNotLoadBaseRule) objBaseRule;

                final boolean isSatisfied = objNotLoadBaseRule
                        .isSatisfiedCondition( getPassedGenericObject(), - 1 );
                if ( isSatisfied ) {
                    this.mButton.setVisibility( View.VISIBLE );
                }
            }
        }
    }

    @Override
    protected void setEventListener() {
        // we set onclick listener for this button
        this.mButton.setOnClickListener( getOnViewClickListener() );
    }

    @Override
    protected void initViewProperties() {
        if ( getControlSpec().getValue() != null ) {
            this.mButton.setText( getControlSpec().getValue() );
        }
        this.mButton.setTag( getControlSpec() );
        final boolean isVisible = getControlSpec().isVisibility();
        if ( isVisible ) {
            this.mButton.setVisibility( View.VISIBLE );
        } else {
            this.mButton.setVisibility( View.GONE );
        }
    }

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        return null;
    }

}
